package com.ifengchao.service.impl;


import com.ifengchao.dao.iface.DealPlatformDao;
import com.ifengchao.pojo.PlatformInfo;
import com.ifengchao.service.DealPlatformService;
import com.ifengchao.util.zzy.ZhiZiYunUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 平台处理
 *
 * @author HuangYL
 */
@Service
public class DealPlatformServiceImpl implements DealPlatformService {

    private static final Logger logger = Logger.getLogger(DealPlatformServiceImpl.class);

    @Autowired(required = true)
    private DealPlatformDao dealPlatformDao;

    /**
     * 验证所有平台
     */
    @Override
    public void verifyZzyPlatform() throws Exception {
        List<PlatformInfo> list = dealPlatformDao.queryAllPlatform();
        JSONObject rspObject = ZhiZiYunUtil.getZhiZiYunData("data/getNetworksByCondition", null);
        String rspStatus = rspObject.getString("status");
        if ("true".equals(rspStatus)) {
            JSONObject resObject = rspObject.getJSONObject("response");
            JSONArray dataArray = resObject.getJSONArray("data");
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject obj = dataArray.getJSONObject(i);
                String platformId = obj.getString("networkid");
                String platformName = obj.getString("name");
                JSONArray typeArray = obj.getJSONArray("creativetype");
                // 与现有平台数据对比
                if (list.size() == 0) {
                    PlatformInfo entity = convert2Entity(platformId, platformName, typeArray);
                    dealPlatformDao.insertPlatformInfo(entity);
                    continue;
                }
                for (int j = 0; j < list.size(); j++) {
                    PlatformInfo pojo = list.get(j);
                    String str = String.valueOf(pojo.getPlatformId());
                    if (platformId.equals(str)) {
                        String arryStr = typeArray.toString();
                        if (!arryStr.equals(pojo.getCreativeType())) {
                            logger.info("该平台创意类型不存在！更新");
                            PlatformInfo entity = convert2Entity(platformId, platformName, typeArray);
                            dealPlatformDao.updatePlatformInfo(entity);
                        }
                        break;
                    }
                    if (j == list.size() - 1) {
                        PlatformInfo entity = convert2Entity(platformId, platformName, typeArray);
                        dealPlatformDao.insertPlatformInfo(entity);
                    }
                }
            }
        }
    }

    public PlatformInfo convert2Entity(String platformId, String platformName, JSONArray typeArray) {
        PlatformInfo entity = new PlatformInfo();
        entity.setPlatformId(Long.parseLong(platformId));
        entity.setPlatformName(platformName);
        entity.setCreativeType(typeArray.toString());
        return entity;
    }

}
